package com.test.burnincash.activity_fragment_burnin.fragment_burnin;

import android.annotation.SuppressLint;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.test.burnincash.R;
import com.test.burnincash.activity_fragment_burnin.ServiceFeedbackActivity_burnin;
import com.test.burnincash.activity_fragment_burnin.base_burnin.BaseActivity_burnin;
import com.test.burnincash.adapter_burnin.CustomerServiceListAdapter_burnin;
import com.test.burnincash.constant_burnin.Constant_burnin;
import com.test.burnincash.dialog_burnin.CustomerServiceDialog_burnin;
import com.test.burnincash.dialog_burnin.LogoutDialog_burnin;
import com.test.burnincash.util_burnin.helper_burnin.UserInfoHelper_burnin;
import com.test.burnincash.util_burnin.http_burnin.HttpManager_burnin;
import com.test.burnincash.activity_fragment_burnin.base_burnin.BaseFragment_burnin;

public class PersonalCenterFragment_burnin extends BaseFragment_burnin {

    private ListView customerServiceLv;

    private CustomerServiceListAdapter_burnin customerServiceListAdapter;
    private BaseActivity_burnin activity;

    private int listHeight = 0;

    public PersonalCenterFragment_burnin(){}

    public PersonalCenterFragment_burnin(BaseActivity_burnin activity){
        this.activity = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_personal_center_burnin, container, false);
        init(view);
        return view;
    }

    @SuppressLint("SetTextI18n")
    private void init(View view){

        customerServiceLv = view.findViewById(R.id.fragment_personal_center_lv_customer_service);

        view.findViewById(R.id.fragment_personal_center_layout_customer_service).setOnClickListener(v -> new CustomerServiceDialog_burnin(requireContext()).show());

        TextView mobileTv = view.findViewById(R.id.fragment_personal_center_tv_mobile);
        mobileTv.setText(Constant_burnin.Mobile_Prefix_Nigeria+" "+UserInfoHelper_burnin.getInstance().getHideMobile());

        //Service Feedback
        view.findViewById(R.id.fragment_personal_center_layout_service_feedback).setOnClickListener(v -> {
            if(activity!=null) activity.startActivity_burnin(ServiceFeedbackActivity_burnin.class);
        });

        //刷新
        view.findViewById(R.id.fragment_personal_center_btn_refresh_status).setOnClickListener(v -> HttpManager_burnin.getInstance().getAppShowInfo());

        //退出登录
        view.findViewById(R.id.fragment_personal_center_btn_logout).setOnClickListener(v -> {
            if (activity!=null) new LogoutDialog_burnin(activity).show();
        });

        customerServiceListAdapter = new CustomerServiceListAdapter_burnin(requireContext());
        customerServiceLv.setAdapter(customerServiceListAdapter);

        updateCustomerService();

    }

    public void updateCustomerService(){

        customerServiceListAdapter.initCustomerService();
        customerServiceListAdapter.notifyDataSetChanged();

        ViewGroup.LayoutParams layoutParams = customerServiceLv.getLayoutParams();
        if (listHeight==0) listHeight = layoutParams.height;
        layoutParams.height = customerServiceListAdapter.getCount()*listHeight;
        customerServiceLv.setLayoutParams(layoutParams);
    }

}
